//app.js

var loginUrl = "https://yjy.beichenhuayu.com/tuikeService/m/api/weixin/weixinLogin";

App({
  onLaunch: function () {
    var that = this;
    that.getUserInfo();
    this.login();
    that.getLocationLongitude();
    that.getLocationLatitude();
  },

  getUserInfo: function () {
    var that = this
    wx.login({
      success: function () {
        wx.getUserInfo({
          success: function (res) {
            var avatarUrl = res.userInfo.avatarUrl;
            var nickName = res.userInfo.nickName;
            wx.setStorageSync('wxAvatarUrl', avatarUrl);
            wx.setStorageSync('wxNickName', nickName);

            wx.request({
              url: that.globalData.domain + that.globalData.subDomain + '/my/saveUser?',
              method: 'post',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                'openid': that.getOpenId(),
                'user.nickname': nickName,
                'user.headimgurl': avatarUrl
              },
              success: function (res) {
                wx.setStorageSync('wxPubApplyStatus', res.data.obj.parkingPubApplyStatus);
                console.log("saveuser:"+res);
              }
            })
          }
        })
      }
    })
  },

  login : function () {
    var that = this;
    var token = that.globalData.token;
    wx.login({
      success: function (res) { 
        if (res.code) {
          console.log(res.code);
          //发起网络请求
          wx.request({
            url: loginUrl,
            data: {
              code: res.code
            },
             success: function (res) {
  
              if (res.data.code == 100) {
                that.globalData.openid = res.data.obj.openid;
                wx.setStorageSync('wxOpenid', res.data.obj.openid);
              }
            }
          })  
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },


  globalData:{
    userInfo:null,
    subDomain: "tz",
    version: "2.0",
    shareProfile: '共享停车', // 首页转发的时候话术
    openid: wx.getStorageSync('wxOpenid'),
    domain:'https://yjy.beichenhuayu.com',
    subDomain:'/tuikeService/m/api'
  },


  getOpenId : function(){
    var that = this;
    var openid = wx.getStorageSync('wxOpenid');
    
    console.log("global="+openid);
    return openid;
  },


  getLocationLatitude:function(){
    var that = this;
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        console.log("Latitude:" + res.latitude);
        wx.setStorageSync('wxLatitude', res.latitude);
      }
    })
  },

  getLocationLongitude: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var longitude = res.longitude;
        console.log("longtitude:" + longitude);
        wx.setStorageSync('wxLongitude', res.longitude);
      }
    })
  },


showModalContent:function(showContent){
  wx.showModal({
    title: '提示',
    content: showCotent,
    success: function (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      } else if (res.cancel) {
      }
    }
  })
}
 


  /*
  根据自己需要修改下单时候的模板消息内容设置，可增加关闭订单、收货时候模板消息提醒；
  1、/pages/to-pay-order/index.js 中已添加关闭订单、商家发货后提醒消费者；
  2、/pages/order-details/index.js 中已添加用户确认收货后提供用户参与评价；评价后提醒消费者好评奖励积分已到账；
  3、请自行修改上面几处的模板消息ID，参数为您自己的变量设置即可。  
   */
})
